<?php
error_reporting(E_ALL);

// config.php has autoload function which will load other classes automatically
require_once '../config/config.php';

// redirect user to loginhome if already logged in
session_start();
if (array_key_exists('useremail', $_SESSION)) {
    header("Location: loginhome.php");
    exit;
}

$logonSuccess = false;
$useremail = '';
$submitted = ($_SERVER['REQUEST_METHOD'] == "POST");

$userManager = new userManager();
static $userDetails;
$userDetails = null;
if ($submitted) {
    $user = new User();
    $user->setEmail(htmlentities($_POST['useremail'], ENT_COMPAT, 'UTF-8'));
    $user->setPassword(htmlentities($_POST['password']));

    $userDetails = $user;
    $logonSuccess = $userManager->verifyUserCredentials($userDetails);
    if ($logonSuccess) {
        session_start();
        $_SESSION["useremail"] = $user->getEmail();
        header("Location: loginhome.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php include('common/headsection.php'); ?>
        <title>iBill - Login</title>
    </head>
    <body>
        <div class="wrapper">
            <?php include('common/header.php'); ?>
            <div class="login">
                <form class="logon" id="frmLogin" name="logon" action="index.php" method="POST">
                    <fieldset>
                        <legend>Login</legend>
                        <table>
                            <tr>
                                <td><label for="useremail">Email</label></td>
                                <td><input type="text" id="useremail" name="useremail" value="<?php echo htmlspecialchars($useremail); ?>"><br/></td>
                            </tr>
                            <tr>
                                <td><label for="password">Password</label></td>
                                <td><input type="password" name="password"><br/></td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <div><span class="error">
                                            <?php
                                            if ($submitted) {
                                                if (!$logonSuccess)
                                                    //echo ihelper::lang("ACCOUNT_EMAIL_OR_PASS_INVALID") .'<br/>';
                                                    echo ihelper::getErrorMessage($userManager->errs, 'verify').'<br/>';
                                            } else {
                                                echo "<br/>";
                                            }
                                            ?>
                                        </span>
                                    </div>
                                </td>
                            <tr>
                                <td colspan="2" align="right">
                                    <button class="button" type="submit">Login</button>
                                </td>
                            </tr>
                        </table>
                        <div>
                            <span class="register">Don't have account?
                                <a href="register.php">Register</a>
                            </span>
                        </div>
                        <div>
                            <span class="forgotpass">
                                <a href="forgotpass.php">Forgot Password?</a>
                            </span>
                        </div>
                    </fieldset>
                </form>
            </div>
            <?php include('common/footer.php'); ?>
        </div>
    </body>
    <script type="text/javascript">
        // place cursor in the first fields
        document.frmLogin.useremail.focus();
    </script>
</html>
